package com.gcl.rabbitmq;

import com.gcl.rabbitmq.utils.RabbtMQUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer02 {


    public static void main(String[] args) throws IOException, TimeoutException {

        Channel channel = RabbtMQUtils.getChannel();

        // 声明直接路由器
        channel.exchangeDeclare(Producer.EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

        // 创建临时队列
        channel.queueDeclare("disk", false, false, false, null);

        // 队列绑定交换机
        channel.queueBind("disk", Producer.EXCHANGE_NAME, "error");


        // 消费消息

        DeliverCallback deliverCallback = (consumeTag, deliver) -> {
            String msg = new String(deliver.getBody());
            File file = new File("C:\\Users\\Anorak\\Desktop\\rabbitmq_loginfo.txt");
            FileUtils.writeStringToFile(file,msg,"UTF-8");
            System.out.println("数据写入文件成功");
        };
        CancelCallback cancelCallback = (consumeTag) -> {};
        channel.basicConsume("disk", true, deliverCallback, cancelCallback);

    }


}
